package controll

import (
	"SalesMangent/function"
	"SalesMangent/models"

	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	DB := function.GetDB()

	requestUser := make(map[string]interface{})
	c.ShouldBind(&requestUser)

	username := requestUser["name"].(string)
	password := requestUser["password"].(string)

	var User models.Stora
	DB.Table("stora").Where("c_code = ?", username).First(&User)
	//两次判断，先判断账号是否正确，后判断密码是否正确
	if User.Ccode == "" {
		c.JSON(200, gin.H{
			"code": 404,
			"msg":  "未找到用户",
		})
		return
	}
	if User.Password != password {
		c.JSON(200, gin.H{
			"code": 422,
			"msg":  "密码错误！",
		})
		return
	}

	//通过说明帐号密码都正确
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
	})

}
